package com.wyd.filter;

import com.wyd.entity.Context;
import com.wyd.entity.DseProductPriorityInfo;
import lombok.extern.slf4j.Slf4j;
import org.jeasy.rules.annotation.Action;
import org.jeasy.rules.annotation.Condition;
import org.jeasy.rules.annotation.Fact;
import org.jeasy.rules.annotation.Rule;

import java.util.Iterator;

@Rule(name = "networktype_filter", description = "网络制式匹配过滤", priority = 4)
@Slf4j
public class NetWorkTypeFilter {

  @Condition
  public boolean when() {
    return true;
  }

  @Action
  public void action(@Fact("ct") Context ct) {
     log.info("开始执行>>>网络制式匹配过滤");
     if(ct.getDseProductList() == null || ct.getDseProductList().isEmpty()){
          return;
     }
     //卡产品支持的网络制式和终端参数plmns（plmn/网络制式/强度）所支持的必须能至少匹配到一个
     if(ct.getNetWorkTypeSet() != null && ct.getNetWorkTypeSet().size() > 0) {
        Iterator<DseProductPriorityInfo> iterator = ct.getDseProductList().iterator();
        while (iterator.hasNext()) {
          DseProductPriorityInfo info = iterator.next();
          boolean flag = false; //匹配标记
          for(Integer net : ct.getNetWorkTypeSet()){
             if((net.intValue() & info.getNetType()) > 0){
               flag = true; //匹配
             }
          }
          if (!flag) {
            iterator.remove();
            log.info("网络制式匹配过滤产品id>>>{},产品网络：{}", info.getDseProductId(),info.getNetType());
          }
        }
     }
  }
}
